package com.idrem.views.report;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EncodingUtils;
import org.apache.http.util.EntityUtils;

import com.idrem.R;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class ReportAct extends Activity {
	private static String TAG = "ReportAct";
	final Activity activity = this;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
		setContentView(R.layout.report);
		WebView webView = (WebView) findViewById(R.id.webView);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setSupportZoom(true);
		webView.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {
				activity.setTitle("Loading...");
				activity.setProgress(progress * 100);
				if (progress == 100)
					activity.setTitle(R.string.app_name);
			}
		});
		webView.setWebViewClient(new WebViewClient() {

			public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
				// Handle the error
			}

			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
		});
//		webView.loadUrl("http://www.baidu.com");
		String URL = "http://59.46.197.188:7001/easd/mobileEntry.do";
        String postData = "type=performance&userpeng.jiang";
        webView.postUrl(URL, EncodingUtils.getBytes(postData, "BASE64"));
	}

	private void getPerformanceResp() {
		try {
			final String SERVER_URL = "http://59.46.197.188:7001/easd/mobileEntry.do"; // 定义需要获取的内容来源地址
			HttpPost request = new HttpPost(SERVER_URL); // 根据内容来源地址创建一个Http请求
			List params = new ArrayList();
			params.add(new BasicNameValuePair("type", "performance")); // 添加必须的参数
			params.add(new BasicNameValuePair("user", "peng.jiang")); // 添加必须的参数
			request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); // 设置参数的编码
			HttpResponse httpResponse = new DefaultHttpClient().execute(request); // 发送请求并获取反馈
			// 解析返回的内容
			if (httpResponse.getStatusLine().getStatusCode() != 404) {
				String result = EntityUtils.toString(httpResponse.getEntity());
				System.out.println(result);
				Log.i(TAG, result);
			}
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
		}
	}

}
